<?php

namespace WechatSDK\Components;

use WechatSDK\Lib\Tools;
require_once ('Common.php');
require_once ('Imessage.php');
require_once ('Dispatcher.php');

/**
 * 消息接收处理组件
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */
class Receive extends Common 
{

    private $receive;
    

    /** 文件过滤 */
    protected $_text_filter = true;
    
    private $dispatcher;
    function __construct()
	{
        $this->dispatcher = Dispatcher::getInstance();
	}
	function __destruct()
	{
        
	}
	public function setRevData($postxml)
	{
        $this->receive = Tools::xmlToArray($postxml);
        $this->dispatcher->forward($this->receive);
        return $this->receive;
	}
    public function getRevData()
    {
        return $this->receive;
    }
	public function getFrom()
	{
        if (isset($this->receive['FromUserName'])) {
            return $this->receive['FromUserName'];
        }
        return false;
	}
	public function getTo()
	{
        if (isset($this->receive['ToUserName'])) {
            return $this->receive['ToUserName'];
        }
        return false;
	}
	public function getMsgType()
	{
        if (isset($this->receive['MsgType'])) {
            return $this->receive['MsgType'];
        }
        return false;
	}
	public function getMsgID()
	{
        if (isset($this->receive['MsgId'])) {
            return $this->receive['MsgId'];
        }
        return false;
	}

	public function getCreatetime()
	{
        if (isset($this->receive['CreateTime'])) {
            return $this->receive['CreateTime'];
        }
        return false;
	}
     /**
     * 获取接收消息内容正文
     * @return bool
     */
    public function getContent()
	{
         if (isset($this->receive['Content'])) {
            return $this->receive['Content'];
        } else if (isset($this->receive['Recognition'])) { //获取语音识别文字内容，需申请开通
            return $this->receive['Recognition'];
        }
        return false;
	}

	public function getPic()
	{
        if (isset($this->receive['PicUrl'])) {
            return array(
                'mediaid' => $this->receive['MediaId'],
                'picurl'  => (string)$this->receive['PicUrl'], //防止picurl为空导致解析出错
            );
        }
        return false;
	}

	public function getLink()
	{
         if (isset($this->receive['Url'])) {
            return array(
                'url'         => $this->receive['Url'],
                'title'       => $this->receive['Title'],
                'description' => $this->receive['Description']
            );
        }
        return false;
	}
    /**
     * 获取接收地理位置
     * @return bool|array
     */
    public function getLocation() {
        if (isset($this->receive['Location_X'])) {
            return array(
                'x'     => $this->receive['Location_X'],
                'y'     => $this->receive['Location_Y'],
                'scale' => $this->receive['Scale'],
                'label' => $this->receive['Label']
            );
        }
        return false;
    }
     /**
     * 获取上报地理位置事件
     * @return bool|array
     */
    public function getLatitude() {
        if (isset($this->receive['Latitude'])) {
            return array(
                'x'         => $this->receive['Latitude'],
                'y'         => $this->receive['Longitude'],
                'precision' => $this->receive['Precision'],
            );
        }
        return false;
    }
    public function getEventAndKey()
	{
         if (isset($this->receive['Event'])) {
            $array['event'] = $this->receive['Event'];
        }
        if (isset($this->receive['EventKey'])) {
            $array['key'] = $this->receive['EventKey'];
        }
        if (isset($array) && count($array) > 0) {
            return $array;
        }
        return false;
	}
    public function getEvent()
	{
       if (isset($this->receive['Event'])) {
            return $this->receive['Event'];
        }
        return false;
	}
    public function getEventKey()
	{
        if (isset($this->receive['EventKey'])) {
            return $this->receive['EventKey'];
        }
        return false;
	}
    /**
     * 获取自定义菜单的扫码推事件信息
     *
     * 事件类型为以下两种时则调用此方法有效
     * Event     事件类型，scancode_push
     * Event     事件类型，scancode_waitmsg
     * @return bool|array
     */
    public function getScanInfo() {
        if (isset($this->receive['ScanCodeInfo'])) {
            if (!is_array($this->receive['ScanCodeInfo'])) {
                $array = (array)$this->receive['ScanCodeInfo'];
                $this->receive['ScanCodeInfo'] = $array;
            } else {
                $array = $this->receive['ScanCodeInfo'];
            }
        }
        if (isset($array) && count($array) > 0) {
            return $array;
        }
        return false;
    }
	public function getVideo()
	{
        if (isset($this->receive['MediaId'])) {
            return array(
                'mediaid'      => $this->receive['MediaId'],
                'thumbmediaid' => $this->receive['ThumbMediaId']
            );
        }
        return false;
	}
    public function getMediaID()
	{
        if (isset($this->receive['MediaId'])) {
           return $this->receive['MediaId'];
        }
        return false;
	}
}
?>